package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.FinanceService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.vo.FinanceVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

//财务报运单管理
@Service
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private FinanceDao financeDao;

    @Autowired
    private PackingDao packingDao;

    @Autowired
    private ShippigOrderDao shippigOrderDao;

    @Autowired
    private ExportDao exportDao;

    @Autowired
    private ExportProductDao exportProductDao;

    @Autowired
    private ExtEproductDao extEproductDao;

    @Autowired
    private InvoiceDao invoiceDao;


    //分页查询
    public PageInfo findAll(int page, int size, FinanceExample example) {
        PageHelper.startPage(page,size);
        List<Finance> list = financeDao.selectByExample(example);
        return new PageInfo(list);
    }

    //新增
    public void save(Finance finance) {
        //1.获取id
        String id = finance.getId();
        //2.根据id查询装箱单
        Packing packing = packingDao.selectByPrimaryKey(id);
        //3.获取报运合同号
        String exportNos = packing.getExportNos();
        finance.setExportNos(exportNos);
        //4.查询委托单信息
        ShippigOrder shippigOrder = shippigOrderDao.selectByPrimaryKey(id);
        String portOfLoading = shippigOrder.getPortOfLoading();
        String portOfDischarge = shippigOrder.getPortOfDischarge();
        finance.setShipmentPort(portOfLoading);
        finance.setDestinationPort(portOfDischarge);
        //5.获取报运单id集合
        String[] exportIds = packing.getExportIds().split(",");
        //6.查询报运单集合
        ExportExample exportExample = new ExportExample();
        ExportExample.Criteria criteria = exportExample.createCriteria();
        criteria.andIdIn(Arrays.asList(exportIds));
        List<Export> exports = exportDao.selectByExample(exportExample);
        Double tax = 0.0;
        Double totalAmount = 0.0;
        Double totalExamount = 0.0;
        //7.遍历报运单
        for (Export export : exports) {
            //获取报运单id
            String exportId = export.getId();
            //设置查询条件
            ExportProductExample epe = new ExportProductExample();
            ExportProductExample.Criteria epec = epe.createCriteria();
            epec.andExportIdEqualTo(exportId);
            //查询报运单货物信息
            List<ExportProduct> exportProducts = exportProductDao.selectByExample(epe);
            //遍历
            for (ExportProduct ep : exportProducts) {
                totalExamount += ep.getExPrice()==null? 0 :ep.getExPrice();
                totalAmount += ep.getPrice()==null? 0 :ep.getPrice();
                tax += ep.getTax()==null? 0 :ep.getTax();

                //根据ep的id查询附件信息
                ExtEproductExample eee = new ExtEproductExample();
                ExtEproductExample.Criteria eeec = eee.createCriteria();
                eeec.andExportProductIdEqualTo( ep.getId());
                List<ExtEproduct> extEproducts = extEproductDao.selectByExample(eee);
                //遍历附加集合
                if(extEproducts!=null && extEproducts.size() != 0) {
                    for (ExtEproduct ee : extEproducts) {
                        totalAmount += ee.getAmount() == null ? 0 : ee.getAmount();
                    }
                }
            }
        }
        finance.setTotalAmount(totalAmount);
        finance.setTotalExamount(totalExamount);
        finance.setTotalTax(tax);
        //保存财务信息
        financeDao.insertSelective(finance);

        //5.修改发票状态
        Invoice invoice = new Invoice();
        invoice.setId(id);
        invoice.setState(1);
        invoiceDao.updateByPrimaryKeySelective(invoice);
    }

    //更新
    public void update(Finance finance) {
        financeDao.updateByPrimaryKeySelective(finance);
    }

    //删除
    public void delete(String id) {
      financeDao.deleteByPrimaryKey(id);
    }

    //根据id查
    public Finance findById(String id) {
        return financeDao.selectByPrimaryKey(id);
    }



    //根据发票查询财务报运单
//    public List<FinanceVo> findByShipTime(String shipTime) {
//        return financeDao.findByShipTime(shipTime);
//    }
}
